package org.bonorsoft.modules.control.controller;

import java.util.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.control.entity.Label;
import org.bonorsoft.modules.control.entity.Staff;
import org.bonorsoft.modules.control.service.ICommonService;
import org.bonorsoft.modules.control.service.IPopulationService;
import org.bonorsoft.modules.control.service.IStaffService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.modules.control.service.IUnitService;
import org.bonorsoft.modules.control.vo.StaffVo;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;

 /**
 * @Description: 实有人口从业状况(从业人员信息）
 * @Author: WilliamYe
 * @Date:   2021-03-05
 * @Version: V1.0
 */
@Slf4j
@Controller
@RequestMapping("/control/staff")
public class StaffController extends BonorController<Staff, IStaffService> {
    private String prefix = "control/staff";

	@Autowired
	private IStaffService staffService;

	@Autowired
	private IUnitService unitService;

	 @Autowired
	 private ICommonService commonService;

	/**
	*首页
	*/
	@RequiresPermissions("control:staff:view")
    @GetMapping()
    public String index()
    {
     return prefix + "/staff";
    }

	/**
	 * 分页列表查询
	 *
	 * @param staff
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "实有人口从业状况(从业人员信息）-分页列表查询")
	@RequiresPermissions("control:staff:list")
	@GetMapping(value = "/list")
	@ResponseBody
	public Result<?> queryPageList(Staff staff,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Staff> queryWrapper = QueryGenerator.initQueryWrapper(staff, req.getParameterMap());
		Page<Staff> page = new Page<Staff>(pageNo, pageSize);
		IPage<StaffVo> pageList = staffService.paging(page, queryWrapper);
		return Result.ok(pageList);
	}

	 /**
	  * 查看从业人员列表
	  */
	 @GetMapping("/getStaff/{id}")
	 public String getStaff(@PathVariable("id") String id,ModelMap map){
		 map.put("id", id);
		 List<StaffVo> staffs = staffService.listing(new QueryWrapper<Staff>().eq("zagldwbm", id));
		 if(staffs.size() > 0) map.put("staffs", staffs);
		 return "control/unit/staff";
	 }

	 /**
	  * 选择地址树
	  */
	 @GetMapping("/selectAddressTree")
	 public String selectAddressTree()
	 {
		 return prefix + "/tree";
	 }

    /**
    * 添加页面
    *
    */
    @GetMapping("/add/{unitId}")
    public String add(@PathVariable("unitId") String unitId, ModelMap map)
    {
     	map.put("unit", unitService.getById(unitId));
		map.put("labelNames", "");
    	return prefix + "/add";
    }

	/**
	 * 添加
	 *
	 * @param staff
	 * @return
	 */
	@AutoLog(value = "实有人口从业状况(从业人员信息）-添加")
	@RequiresPermissions("control:staff:add")
	@PostMapping(value = "/add")
    @ResponseBody
	public Result<?> add(@Validated Staff staff, String photo) {
		String id = UUID.randomUUID().toString().replaceAll("-", "");
		staffService.save(staff.setId(id));
		if(oConvertUtils.isNotEmpty(photo)){
			commonService.addPhoto(id, photo);
		}
		return Result.ok("添加成功！");
	}

    /**
    * 编辑页面
    */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map)
    {
		Staff staff = staffService.getById(id);
		map.put("staff", staff);
		map.put("photo", commonService.getPhoto(id));
		map.put("labelNames","");
		if(oConvertUtils.isNotEmpty(staff.getBq())){
			List<Label> labelList = staffService.getlabel(id);
			StringJoiner joiner = new StringJoiner(",");
			labelList.forEach(item -> joiner.add(item.getLabelName()));
			map.put("labelNames", joiner.toString());
		}
		return prefix + "/edit";
    }

	/**
	 * 编辑
	 *
	 * @param staff
	 * @return
	 */
	@AutoLog(value = "实有人口从业状况(从业人员信息）-编辑")
	@RequiresPermissions("control:staff:edit")
	@PostMapping(value = "/edit")
	@ResponseBody
	public Result<?> edit(@Validated Staff staff, String photo) {
		staff.setGxsj(new Date());
		staffService.updateById(staff);
		if(oConvertUtils.isNotEmpty(photo)){
			commonService.updatePhoto(staff.getId(), photo);
		}
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "实有人口从业状况(从业人员信息）-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		staffService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "实有人口从业状况(从业人员信息）-批量删除")
	@RequiresPermissions("control:staff:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	@ResponseBody
	@Transactional
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		List<String> list = Arrays.asList(ids.split(","));
		this.staffService.removeByIds(list);
		commonService.removeByIds(list);
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "实有人口从业状况(从业人员信息）-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Staff staff = staffService.getById(id);
		return Result.ok(staff);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param staff
    */
    @AutoLog(value = "实有人口从业状况(从业人员信息）-导出excel")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Staff staff) {
      return super.exportXls(request, staff, Staff.class, "实有人口从业状况(从业人员信息）");
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @AutoLog(value = "实有人口从业状况(从业人员信息）-通过excel导入数据")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, Staff.class);
    }

    /**
     * 模板下载
     * @return
     */
    @AutoLog(value = "实有人口从业状况(从业人员信息）-模板下载", operateType = 6)
    @RequestMapping(value = "/importTemplate")
    public ModelAndView importTemplate() {
        return super.importTemplateXls(Staff.class ,"实有人口从业状况(从业人员信息）");
    }

}
